package com.ruoyi.logistic.service.impl;


import com.alibaba.fastjson2.JSONObject;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.github.pagehelper.util.StringUtil;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;

import com.ruoyi.logistic.utils.RandomUtil;
import com.ruoyi.logistic.utils.SMSVerificationUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class KSMSServiceImpl {
    @Autowired
    private RedisCache redisCache;

    private String app_id = "";
    private String app_secret = "";
    private String app_template_id_invite = "";
    private String app_template_id_random = "";
    public static final String SMS_ALI = "aliyun";
    public static final String SMS_TTGJ = "ttgj";


    public R isAvailable(String telephone, String randcode) {
        // 验证码有效次数维护
        String key = telephone;
        String _randcode = redisCache.getCacheObject(telephone + randcode);
//        String _randcode = "123456";
        if (StringUtil.isEmpty(_randcode)) {
            return R.fail("无验证码，请重新获取验证码");
        }
        JSONObject parseObject = JSONObject.parseObject(_randcode);
        Object effectiveCode = parseObject.get(telephone);
        if (randcode.equals(effectiveCode)) {
            return R.fail("验证码错误，请重新输入");
        } else {
            return R.ok();
        }
    }

    public String sendSmsToInternational(String telephone, String areaCode, String language, String code) {
        String key = telephone + code;
        Long ttl = redisCache.getExpire(key);
        code = redisCache.getCacheObject(key);
        if (ttl > 540) {
            String msg = "";
            msg = MessageFormat.format(msg, ttl - 540);
            log.info("msg : " + msg);
            throw new ServiceException(msg);
        }
        if (null == code){
            code = RandomUtil.randomCode();
            key = telephone + code;
        }
        else {
            JSONObject parseObject = JSONObject.parseObject(code);
            code = parseObject.getString(telephone);
            log.info(" redis code is : {}", code);
        }
        final String smsCode = code;

        //先关闭手机验证码发送功能
//        aliSMS(telephone, language, smsCode, areaCode, key);

        redisCache.setCacheObject(key, smsCode, 5, TimeUnit.MINUTES);

        return code;
    }

    // 阿里云短信服务
    public void aliSMS(String telephone, String language, String smsCode, String areaCode, String key) {
        try {
            SendSmsResponse sendSms = SMSVerificationUtils.sendSms(telephone, smsCode, areaCode);
            if (null != sendSms && "OK".equals(sendSms.getCode())) {
                redisCache.setCacheObject(key, smsCode, 5, TimeUnit.MINUTES);
            }
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
